package com.supermap.search.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by admin on 2017/9/26 0026.
 */

public abstract class DefaultAdapter<T> extends BaseAdapter {
    private List<T> datas;

    public DefaultAdapter(List<T> datas) {
        this.datas = datas;
    }

    public void SetDataList(List<T> datas){
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    // 条目的数量
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 条目显示的view对象
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseHolder<T> holder = null;
        if (convertView == null) {
            holder = getHolder();
        } else {
            holder = (BaseHolder<T>) convertView.getTag();
        }

        T t = datas.get(position);
        holder.setData(t);
        return holder.getContentView();
    }

    /**
     * 获取holder对象
     *
     * @return
     */
    public abstract BaseHolder<T> getHolder();
}
